﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CApAPPGunOnlyObjectFlowTask
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands;

public class CApAPPGunOnlyObjectFlowTask : CApAPPObjectFlowTask
{
  private TxTransformation m_initialGripLocation;

  public CApAPPGunOnlyObjectFlowTask(ITxOperation i_gunOnlyWeldOperation)
    : base(i_gunOnlyWeldOperation)
  {
    this.fillTask(i_gunOnlyWeldOperation);
  }

  public override TxObjectList GetAllLocations()
  {
    TxObjectList allLocations = new TxObjectList();
    for (int index = 0; index < ((ITxObjectCollection) (this.Operation as ITxOrderedObjectCollection)).Count; ++index)
    {
      ITxObject childAt = (this.Operation as ITxOrderedObjectCollection).GetChildAt(index);
      ((Collection<ITxObject>) allLocations).Add(childAt);
    }
    return allLocations;
  }

  public ITxWeldOperation WeldOperation => this.Operation as ITxWeldOperation;

  private ITxLocatableObject SimulatedObject
  {
    get
    {
      ITxWeldOperation weldOperation = this.WeldOperation;
      return (weldOperation != null ? ((IEnumerable<ITxObject>) ((ITxOperation) weldOperation).SimulatedObjects).FirstOrDefault<ITxObject>() : (ITxObject) null) as ITxLocatableObject;
    }
  }

  public override void fillTask(ITxOperation i_gunOnlyWeldOperation)
  {
    if (i_gunOnlyWeldOperation == null)
      return;
    TxObjectList operationLocations = CApAPPUtilities.GetOperationLocations(i_gunOnlyWeldOperation);
    int num = 0;
    List<KiAppObjectFlowWaypointEx> objectFlowWaypointExList = new List<KiAppObjectFlowWaypointEx>();
    foreach (ITxObject i_location in (Collection<ITxObject>) operationLocations)
    {
      if (i_location is ITxLocationOperation)
      {
        bool flag1 = false;
        bool flag2 = false;
        string name = i_location.Name;
        KiAppLocationEx locationFromTxLoc = CApAPPGunOnlyObjectFlowTask.createAppLocationFromTxLoc(i_location as ITxLocationOperation);
        if (num == 0)
        {
          flag2 = true;
          flag1 = true;
        }
        else if (num == ((Collection<ITxObject>) operationLocations).Count - 1)
        {
          flag2 = true;
          flag1 = true;
        }
        switch (i_location)
        {
          case TxWeldLocationOperation _:
          case TxRoboticSeamOperation _:
            flag1 = true;
            flag2 = true;
            break;
        }
        KiAppWaypointStatus appWaypointStatus = (KiAppWaypointStatus) 0;
        KiAppObjectFlowWaypointEx objectFlowWaypointEx = new KiAppObjectFlowWaypointEx(locationFromTxLoc, name, flag2, appWaypointStatus);
        ((KiAppWaypointEx) objectFlowWaypointEx).Locked = flag1;
        objectFlowWaypointExList.Add(objectFlowWaypointEx);
        ++num;
      }
    }
    this.KiTask = (KiAppTaskEx) new KiAppGunOnlyObjectFlowTaskEx(i_gunOnlyWeldOperation as ITxWeldOperation, (IList<KiAppObjectFlowWaypointEx>) objectFlowWaypointExList);
    this.SynchronizedWaypointList = this.Waypoints();
  }

  protected override bool IsLockedLocation(ITxObject i_location)
  {
    return i_location is TxWeldLocationOperation || i_location is TxRoboticSeamOperation;
  }

  private static KiAppLocationEx createAppLocationFromTxLoc(ITxLocationOperation i_location)
  {
    return new KiAppLocationEx()
    {
      AbsoluteLocation = (i_location as ITxLocatableObject).AbsoluteLocation
    };
  }

  public override void AddLocationFromWaypoint(
    CApAPPWaypoint i_waypoint,
    ITxObject i_previousLocation)
  {
    TxObjectFlowLocationOperationCreationData operationCreationData = new TxObjectFlowLocationOperationCreationData();
    TxRoboticViaLocationOperation locationOperation = this.CreateViaLocationOperation(i_waypoint.StoredApproachLocation);
    if (!TxRoboticViaLocationOperation.op_Inequality(locationOperation, (ITxObject) null))
      return;
    locationOperation.Name = i_waypoint.Name;
    locationOperation.AbsoluteLocation = i_waypoint.StoredApproachLocation.AbsoluteLocation;
    if (i_previousLocation == null)
      return;
    ((ITxOrderedObjectCollection) this.WeldOperation).MoveChildAfter((ITxObject) locationOperation, i_previousLocation);
  }

  public TxRoboticViaLocationOperation CreateViaLocationOperation(KiAppLocationEx appLocation)
  {
    TxTransformation absoluteLocation = appLocation.AbsoluteLocation;
    return ((ITxRoboticViaLocationOperationCreation) this.WeldOperation).CreateRoboticViaLocationOperation(new TxRoboticViaLocationOperationCreationData()
    {
      AbsoluteLocation = absoluteLocation
    });
  }

  public override CApAPPWaypoint Waypoint(int i_index)
  {
    CApAPPWaypoint i_wp = new CApAPPWaypoint(((KiAppTaskEx) this.KiGunOnlyObjectFlowTask).GetWaypoint(i_index));
    this.configureWaypoint(i_wp, i_index);
    return i_wp;
  }

  public override void Waypoint(int i_index, CApAPPWaypoint i_wp)
  {
    ((KiAppTaskEx) this.KiGunOnlyObjectFlowTask).SetWaypoint(i_index, i_wp.KiWaypoint);
  }

  private KiAppGunOnlyObjectFlowTaskEx KiGunOnlyObjectFlowTask
  {
    get => this.KiTask as KiAppGunOnlyObjectFlowTaskEx;
  }

  public override void SetGripLocation(TxTransformation newLocation)
  {
    if (this.SimulatedObject == null)
      return;
    this.SimulatedObject.AbsoluteLocation = newLocation;
  }

  public override TxTransformation InitialGripLocation
  {
    get
    {
      if (TxTransformation.op_Equality(this.m_initialGripLocation, (TxTransformation) null))
        this.m_initialGripLocation = this.SimulatedObject?.AbsoluteLocation ?? new TxTransformation();
      return this.m_initialGripLocation;
    }
    set => this.m_initialGripLocation = value;
  }

  public override void ResetGripPosition()
  {
    if (this.SimulatedObject == null)
      return;
    this.SimulatedObject.AbsoluteLocation = this.InitialGripLocation;
  }
}
